package com.xayy.mgt.controller;

import cn.kawins.base.result.ResultBody;
import cn.kawins.mybatis.base.BaseController;
import com.github.pagehelper.PageInfo;
import com.xayy.mgt.model.AnticipatoryFailure;
import com.xayy.mgt.model.HistoryInfluence;
import com.xayy.mgt.model.NodeTreeInfo;
import com.xayy.mgt.model.vo.AnticipatoryFailurePageVo;
import com.xayy.mgt.model.vo.InfluenceIndexVo;
import com.xayy.mgt.service.AnticipatoryFailureService;
import com.xayy.mgt.service.HistoryInfluenceService;
import com.xayy.mgt.service.NodeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/yxgzyx1")
@Api(tags = "预想故障影响")
public class AnticipatoryFailureController extends BaseController<AnticipatoryFailure> {
    @Resource
    private AnticipatoryFailureService anticipatoryFailureService;

    @Resource
    private HistoryInfluenceService historyInfluenceService;

    @Resource
    private NodeInfoService nodeInfoService;

    @PostMapping("/anticipatoryFailurePageList")
    @ApiOperation(value = "预想故障(分页)", httpMethod = "POST", notes = "历史故障影响-根据parentId分页预想故障")
    public ResultBody anticipatoryFailurePageList(@RequestBody AnticipatoryFailurePageVo pageVo) {
        PageInfo<AnticipatoryFailure> page = anticipatoryFailureService.pageList(pageVo);
        return ResultBody.success(page);
    }

    @PostMapping("/saveAnticipatoryFailure")
    @ApiOperation(value = "新增预想故障", httpMethod = "POST", notes = "故障影响-新增预想故障")
    public ResultBody saveAnticipatoryFailure(@RequestBody AnticipatoryFailure anticipatoryFailure) {
        return anticipatoryFailureService.saveAnticipatoryFailure(anticipatoryFailure).body();
    }


    @PostMapping("/editAnticipatoryFailure")
    @ApiOperation(value = "编辑预想故障", httpMethod = "POST", notes = "故障影响-编辑预想故障")
    public ResultBody editAnticipatoryFailure(@RequestBody AnticipatoryFailure anticipatoryFailure) {
        return anticipatoryFailureService.editAnticipatoryFailure(anticipatoryFailure).body();
    }


    @DeleteMapping("/deleteAnticipatoryFailure/{faultId}")
    @ApiOperation(value = "删除预想故障", httpMethod = "DELETE", notes = "故障影响-删除预想故障")
    public ResultBody deleteAnticipatoryFailure(@PathVariable Long faultId) {
        return anticipatoryFailureService.deleteAnticipatoryFailure(faultId).body();
    }

    @GetMapping("/selectOneAnticipatoryDetail/{id}")
    @ApiOperation(value = "查询预想故障(单个详情)", httpMethod = "GET", notes = "故障影响-查询预想故障(单个详情)")
    public ResultBody selectOneAnticipatoryDetail(@PathVariable Long id) {
        return anticipatoryFailureService.selectOneAnticipatoryDetail(id).body();
    }


    @PostMapping("/saveInfluence")
    @ApiOperation(value = "新增影响数据", httpMethod = "POST", notes = "故障影响-新增影响数据")
    public ResultBody saveInfluence(@RequestBody HistoryInfluence historyInfluence) {
        NodeTreeInfo nodeTreeInfo = nodeInfoService.selectOne(NodeTreeInfo.builder().id(Long.valueOf(historyInfluence.getComponents())).build());
        historyInfluence.setComponents(nodeTreeInfo.getNodeName());
        historyInfluence.setComponentsId(nodeTreeInfo.getId());
        return historyInfluenceService.saveInfluence(historyInfluence).body();
    }


    @PostMapping("/editInfluence")
    @ApiOperation(value = "编辑影响数据", httpMethod = "POST", notes = "故障影响-编辑影响数据")
    public ResultBody editInfluence(@RequestBody HistoryInfluence historyInfluence) {
        return historyInfluenceService.editInfluence(historyInfluence).body();
    }


    @DeleteMapping("/deleteInfluence/{id}")
    @ApiOperation(value = "删除影响数据", httpMethod = "DELETE", notes = "故障影响-删除影响数据")
    public ResultBody deleteInfluence(@PathVariable Long id) {
        return historyInfluenceService.deleteInfluence(id).body();
    }

    @GetMapping("/selectInfluence/{id}")
    @ApiOperation(value = "查询影响数据", httpMethod = "GET", notes = "故障影响-查询影响数据")
    public ResultBody selectInfluence(@PathVariable Long id) {
        return historyInfluenceService.selectInfluence(id).body();
    }


    @PostMapping("/selectInfluenceIndex")
    @ApiOperation(value = "查询影响指标", httpMethod = "POST", notes = "故障影响-查询影响指标")
    public ResultBody selectInfluenceIndex(@RequestBody InfluenceIndexVo influenceIndexVo) {
        return historyInfluenceService.selectInfluenceIndexs(influenceIndexVo).body();
    }

    @GetMapping("/selectOneInfluenceIndexDetail/{id}")
    @ApiOperation(value = "查询影响数据(单个详情)", httpMethod = "GET", notes = "故障影响-查询影响数据(单个详情)")
    public ResultBody selectOneInfluenceIndexDetail(@PathVariable Long id) {
        return historyInfluenceService.selectOneInfluenceIndexDetail(id).body();
    }
}
